package com.linkgie.galaxyframework.httpproxy.core;

import java.net.URI;
import java.nio.ByteBuffer;
import java.util.Properties;

import com.linkgie.galaxyframework.httpproxy.HttpMethod;
import com.linkgie.galaxyframework.httpproxy.HttpParameters;
import com.linkgie.galaxyframework.httpproxy.HttpParams;
import com.linkgie.galaxyframework.httpproxy.NameValues;

/**
 * 请求信息；
 * <p>
 * 请求信息 {@link ServcieHttpRequest} 是一次服务调用需要发送的数据的模型，对应地它会被转换 HTTP 协议的一个请求对象；
 * 
 * @author haiq
 *
 */
class ServcieHttpRequest
		implements ServiceRequest, com.linkgie.galaxyframework.httpproxy.ServiceRequest {

	private HttpMethod method;

	private URI uri;

	private ByteBuffer body;

	private PropertyNameValues headers;

	private HttpParams requestParams;

	private Object[] args;

	public ServcieHttpRequest(HttpMethod method, URI uri, HttpParams requestParams, ByteBuffer body, Object[] args) {
		this.method = method;
		this.uri = uri;
		this.headers = new PropertyNameValues();
		this.requestParams = requestParams == null ? new HttpParams() : requestParams;
		this.body = body;
		this.args = args;
	}

	public void setParams(Properties params) {
		for (String name : params.stringPropertyNames()) {
			requestParams.set(name, params.getProperty(name));
		}
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see my.utils.http.agent.Request#getMethod()
	 */
	@Override
	public HttpMethod getMethod() {
		return method;
	}

	@Override
	public HttpParams getParams() {
		return requestParams;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see my.utils.http.agent.Request#getUri()
	 */
	@Override
	public URI getUri() {
		return uri;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see my.utils.http.agent.Request#getBody()
	 */
	@Override
	public ByteBuffer getBody() {
		return body;
	}

	public void setHeader(String name, String value) {
		headers.getProperties().setProperty(name, value);
	}

	public Properties getHeaders() {
		return headers.getProperties();
	}

	@Override
	public Object[] getArguments() {
		return args;
	}

	@Override
	public HttpMethod getHttpMethod() {
		return method;
	}

	@Override
	public HttpParameters getHttpParameters() {
		return requestParams;
	}

	@Override
	public NameValues getHttpHeaders() {
		return headers;
	}

}